package threshold;

/**
 * @Author wujiaming
 * @Description TODO
 * @Date 2023-02-28 21:53
 * @Version 1.0
 */
public class RomanToInt {
    public int romanToInt(String s){
        int num = 0,sum=0;
        int preNUm = getVal(s.charAt(0));
        for(int i=1;i<s.length();i++){
            num = getVal(s.charAt(i));
            if(num<=preNUm){
                sum +=preNUm;
            }else{
                sum -=preNUm;
            }
            preNUm = num;
        }
        sum +=preNUm;
        return sum;
    }

    public int getVal(char ch){
        switch (ch){
            case 'I':return 1;
            case 'V':return 5;
            case 'X':return 10;
            case 'C':return 100;
            case 'L':return 50;
            case 'D':return 500;
            case 'M':return 1000;
            default: return 0;
        }
    }

    public static void main(String[] args) {
        RomanToInt romanToInt = new RomanToInt();
        System.out.println(romanToInt.romanToInt("XXIV"));
    }
}
